import { Hub } from "@/func";
import { Player } from "@minecraft/server";
import { PillarOfFortuneMap } from "./map";
import { PillarOfFortuneContext, PillarOfFortuneModule } from "./module";
import { PillarOfFortuneLobbyState } from "./states/lobby";
import { EntityTypeIds } from "@sapi-game/utils/vanila-data";

export class PillarOfFortune extends PillarOfFortuneModule.Engine<{
    players: Player[];
    map: PillarOfFortuneMap;
}> {
    protected override buildContext(config: {
        players: Player[];
        map: PillarOfFortuneMap;
    }): PillarOfFortuneContext {
        return new PillarOfFortuneContext(
            this.groupBuilder.fromPlayers(config.players),
            { map: config.map }
        );
    }

    override onStart(): void {
        this.pushState(PillarOfFortuneLobbyState);
    }

    override onStop(): void {
        this.context.players.forEach((p) => {
            Hub(p.player);
        });
        //删除所有实体
        const entites = this.context.map.region.getEntitesInRegion({
            excludeTypes: [EntityTypeIds.Player],
        });
        entites.forEach((e) => e.remove());
    }
}
